﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{
    public class SysConfigBLL : BaseBLL<SysConfig, Guid>
    {
        /// <summary>
        /// 商品分类,类型-规格
        /// </summary>
        public static Tuple<string, string[]>[] GoodsClass { get; private set; }
        /// <summary>
        /// 库存文字显示，使用《=依次比较
        /// </summary>
        public static Tuple<int, string>[] AmountDisplay { get; private set; }

        /// <summary>
        /// 省市信息
        /// </summary>
        public static District[] Districts { get; private set; }

        public   Guid? ApprovalAdmin()
        {
           var re= Rep.Entities.FirstOrDefault(p => p.Key == "ApprovalAdmin").Value;
            Guid id;
            if (Guid.TryParse(re,out id))
            {
                 return id;
            }
            else
            {
                return null;
            }
           
        }

        static SysConfigBLL()
        {
            //初始话GoodsClass，AmountDisplay,Districts
            var gList = new List<Tuple<string, string[]>>();
            var bll = BllFactory.GetBll<BaseBLL<SysConfig, Guid>>();
            var typeList = bll.GetList(p => p.Group == "产品类型");
            Tuple<string, string[]> tt = null;
            foreach (var item in typeList)
            {
                tt = Tuple.Create(item.Key, bll.GetList(p => p.Group == "规格" && p.Value2 == item.Key).Select(p => p.Key).ToArray());
                gList.Add(tt);
            }
            GoodsClass = gList.ToArray();
            Districts = BllFactory.GetBll<BaseBLL<District, Guid>>().GetList(p => p.Lv == 1);
            //库存概要显示
            var list = BllFactory.GetBll<BaseBLL<SysConfig, Guid>>().GetList(p => p.Group == "库存概要显示").OrderBy(p => p.Key).ToArray();
            AmountDisplay = list.Select(p => Tuple.Create(int.Parse(p.Value), p.Value2)).ToArray();
        }

        public IEnumerable<SysConfig> SelectByGroup(string group)
        {
            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(group))
            {
                query = query.Where(p => p.Group == group);
            }
            return query.OrderByDescending(p => p.Id).ToList();
        }

        public object[][] GetTableView(string tabName,bool isHander=true)
        {
 
                 var rep= RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
          var dt=  rep.GetTableInfo("select * from NowDepotProfit order by 仓库");
            var list = new List<object[]>();
            if (isHander)
            {
                var cols = dt.Columns.Cast<DataColumn>().Select(p => (object) p.ColumnName).ToArray();
                list.Add(cols);
            }

            var data = dt.Rows.Cast<DataRow>().Select(p=>p.ItemArray).ToArray();
            list.AddRange(data);
            return list.ToArray();
        }
    }
}
